package com.usian.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.google.common.collect.Maps;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.common.AppJwtUtil;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    @Override
    public WmUser saveWmUser(WmUser wmUser) {
        if(wmUser==null){
            return null;
        }
        wmUser.setCreatedTime(new Date());
        boolean b = this.save(wmUser);
        if(b){
            return wmUser;
        }else{
            return null;
        }
    }

    @Override
    public WmUser findByName(String name) {
        if(StringUtils.isBlank(name)){
            return null;
        }

//        LambdaQueryWrapper<WmUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(WmUser::getName,name);
//
//        QueryWrapper<WmUser> queryWrapper =new QueryWrapper<>();
//        queryWrapper.lambda().eq(WmUser::getName,name);
//
//        Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name);

        List<WmUser> wmUserList = this.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        if(wmUserList.size()>0){
            return  wmUserList.get(0);
        }
        return null;
    }

    //自媒体用户登录
    @Override
    public ResponseResult login(WmUserDto wmUserDto) {
        //1.参数校验
        if (StringUtils.isEmpty(wmUserDto.getName()) || StringUtils.isEmpty(wmUserDto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "用户名或密码不能为空");
        }
        Wrapper wrapper = new QueryWrapper<WmUser>();
        ((QueryWrapper) wrapper).eq("name", wmUserDto.getName());

            List<WmUser> list = list(wrapper);

        if (list != null && list.size() == 1) {
            WmUser wmUser = list.get(0);
            String pswd = DigestUtils.md5DigestAsHex((wmUserDto.getPassword() + wmUser.getSalt()).getBytes());

            if (wmUser.getPassword().equals(pswd)) {
                Map<String, Object> map = Maps.newHashMap();
                wmUser.setPassword("");
                wmUser.setSalt("");
                map.put("token", AppJwtUtil.getToken(wmUser.getId().longValue()));
                map.put("user", wmUser);
                return ResponseResult.okResult(map);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }
    }

}

